findAllTab();

/**
 * 弹框隐藏后，重置
 */
$('#Add_Tab_Modal').on('hidden.bs.modal', function (e) {

    $("#tab_name").val('');
    $("#tab_value").selectpicker("val",-1);

})

/**
 * 查询所有标签
 */
function findAllTab() {
    $.ajax({
        url: baseUrl.base + '/customTab/getTabList',
        type: 'post',
        data: null,
        success: function (data) {
            var code = data.code;
            var tabs = data.results;

            if (code == '0') {
                $("#tbody").empty();

                $("#table tbody").html("");

                for (var i = 0; i < tabs.length; i++) {
                    var tab = tabs[i];

                    var tr = document.createElement("tr");

                    var td = document.createElement("td");
                    var text = document.createTextNode(tab.tabName);
                    td.appendChild(text);
                    tr.appendChild(td);

                    var td = document.createElement("td");
                    var str = "";
                    if (tab.tabType == 0) {
                        str = "赛事";
                    } else if (tab.tabType == 1) {
                        str = "比赛";
                    } else if (tab.tabType == 2) {
                        str = "国家";
                    }
                    var text = document.createTextNode(str);
                    td.appendChild(text);
                    tr.appendChild(td);

                    var td = document.createElement("td");
                    var btn1 = document.createElement("button");
                    btn1.setAttribute("type", "button");
                    btn1.setAttribute("class", "btn btn-info btn-sm");
                    btn1.setAttribute("data-toggle", "modal");
                    btn1.setAttribute("data-target", "#Edit_Tab_Modal");
                    btn1.setAttribute("tab_id", tab.tabId);
                    btn1.setAttribute("tab_name", tab.tabName);
                    btn1.setAttribute("tab_type", tab.tabType);

                    btn1.onclick = function () {
                        editTabAction(this);
                    }

                    var tdContent = document.createTextNode("编辑");
                    btn1.appendChild(tdContent);
                    td.appendChild(btn1);

                    var btn2 = document.createElement("button");
                    btn2.setAttribute("type", "button");
                    btn2.setAttribute("tabId", tab.tabId);

                    if (tab.isEnable == 1) {
                        //已禁用
                        btn2.setAttribute("class", "btn btn-danger btn-sm");
                        var tdContent = document.createTextNode("已禁用");
                        btn2.setAttribute("status", 0);
                        btn2.onclick = function (ev) {
                            setDisableAction(this);
                        }
                    } else {
                        //已启用
                        btn2.setAttribute("class", "btn btn-info btn-sm");
                        var tdContent = document.createTextNode("已启用");
                        btn2.setAttribute("status", 1);
                        btn2.onclick = function (ev) {
                            setDisableAction(this);
                        }
                    }

                    btn2.appendChild(tdContent);
                    td.appendChild(btn2);
                    tr.appendChild(td);

                    document.getElementById("tbody").appendChild(tr);
                    document.getElementById("tbody").innerHTML;
                }
            }
        }
    });
}

/**
 * 编辑标签
 */

function editTabAction(element) {
    var tabId = element.getAttribute("tab_id");
    var tabName = element.getAttribute("tab_name");
    var tabType = element.getAttribute("tab_type");

    $('#edit_tab_id').html(tabId);
    $('#edit_tab_name').val(tabName);
    $("#edit_tab_value").selectpicker('val',tabType);
}

/**
 * 保存编辑标签
 */
function editTabActionSave() {
    var tab_name = $('#edit_tab_name').val();
    var tab_value = $('#edit_tab_value').val();
    var tab_id = $('#edit_tab_id').text();

    var data = {
        'tabId': tab_id,
        'tabName': tab_name,
        'tabType': tab_value,
    }

    $.ajax({
        url: baseUrl.base + 'customTab/setTab',
        type: 'post',
        data: data,
        success: function (data) {
            if (data.code == 0) {
                //刷新列表

                showNotification('bottom', 'right', '保存成功');

                findAllTab();
                $("#Edit_Tab_Modal").modal('hide');
            }
        }
    });
}

/**
 * 禁用启用标签
 */

function setDisableAction(element) {
    var data = {
        'tabId': element.getAttribute("tabId"),
        'isEnable': element.getAttribute("status"),
    }

    $.ajax({
        url: baseUrl.base + 'customTab/setTab',
        type: 'post',
        data: data,
        success: function (data) {
            if (data.code == 0) {

                if (element.getAttribute("status") == 1) {
                    //已禁用变为已启用
                    element.setAttribute("class", "btn btn-danger btn-sm");
                    element.setAttribute("status", 0);
                    element.onclick = function (ev) {
                        setDisableAction(this);
                    }
                    element.innerText = "已禁用";
                    showNotification('bottom', 'right', '禁用成功');
                } else {
                    ////已启用变为禁用
                    element.setAttribute("class", "btn btn-info btn-sm");
                    element.setAttribute("status", 1);
                    element.onclick = function (ev) {
                        setDisableAction(this);
                    }
                    element.innerText = "已启用";
                    showNotification('bottom', 'right', '已启成功');
                }

            }
        }
    });
}

/**
 * 添加标签
 */
function add_tab() {
    var tab_name = $('#tab_name').val();
    var tab_value = $('#tab_value').val();

    if (null == tab_name || "" == tab_name) {
        showNotification('bottom', 'right', '请输入标签名称');
        return;
    }
    if (null == tab_value || "" == tab_value) {
        showNotification('bottom', 'right', '请选择标签状态');
        return;
    }

    var data = {
        'tabName': tab_name,
        'tabType': tab_value,
        'isEnable': '0'
    }
    $.ajax({
        url: baseUrl.base + '/customTab/setTab',
        type: 'post',
        data: data,
        success: function (data) {
            var code = data.code;
            if (code == '0') {
                //创建成功
                $('#Add_Tab_Modal').modal('hide');
                showNotification('bottom', 'right', '添加成功');
                findAllTab();
            } else {
                //创建失败
                showNotification('bottom', 'right', '添加失败');
            }
        }
    });
}